package cn.explink.dop.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.explink.dop.entity.vo.SystemParamVO;
import cn.explink.eap.core.common.dao.impl.GenericBaseCommonDao;

@Repository("systemParamDao")
public class SystemParamDao extends GenericBaseCommonDao<SystemParamVO, String> {
	/**
	 *
	 * @Title: addParam
	 * @Description: 保存参数
	 * @author 孟凡岭
	 * @param @param sys
	 * @return void 返回类型
	 * @throws
	 */
	public void addParam(SystemParamVO sys) {
		// TODO Auto-generated method stub
		JdbcTemplate jt = this.getJdbcTemplate();
		String sql = "insert into bd_system_param(name,value) values(?,?)";
		jt.update(sql, new Object[] { sys.getName(), sys.getValue() }, new int[] { Types.VARCHAR, Types.VARCHAR });
	}

	/**
	 *
	 * @Title: updateParam
	 * @Description: 更新参数
	 * @author 孟凡岭
	 * @param @param id
	 * @param @param name
	 * @param @param value
	 * @return void 返回类型
	 * @throws
	 */
	public void updateParam(String id, String name, String value) {
		// TODO Auto-generated method stub
		String sql = "update bd_system_param set name=?,value=? where id=?";
		this.getJdbcTemplate().update(sql, name, value, id);
	}

	/**
	 *
	 * @Title: deleteParam
	 * @Description: 删除参数
	 * @author 孟凡岭
	 * @param @param idList
	 * @return void 返回类型
	 * @throws
	 */
	public void deleteParam(final List<String> idList) {
		// TODO Auto-generated method stub
		String sql = "delete from bd_system_param where id=?";
		this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				// TODO Auto-generated method stub
				ps.setString(1, idList.get(i));
			}

			@Override
			public int getBatchSize() {
				// TODO Auto-generated method stub
				return idList.size();
			}
		});
	}

	/**
	 *
	 * @Title: findDataByPage
	 * @Description: 分页查询数据
	 * @author 孟凡岭
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @return
	 * @return Map<String, Object> 返回类型
	 * @throws
	 */
	public Map<String, Object> findDataByPage(int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer allFromSql = new StringBuffer("from SystemParamVO ");
		StringBuffer countFromSql = new StringBuffer("select count(id) from SystemParamVO ");
		StringBuffer whereSql = new StringBuffer();

		Map<String, Object> map = new HashMap<String, Object>();

		Query queryCount = this.getSession().createQuery((countFromSql.append(whereSql).toString()));
		Integer count = ((Long) queryCount.uniqueResult()).intValue();

		map.put("count", count);

		String sql = (allFromSql.append(whereSql).toString());

		@SuppressWarnings("unchecked")
		List<SystemParamVO> areaList = this.getSession().createQuery(sql).setFirstResult((pageNum - 1) * pageSize).setMaxResults(pageSize).list();
		map.put("list", areaList);
		return map;
	}
}
